﻿
(function ($) {
    /*-------------------------------------------------------------使用者是$("#regionInner").find(".piece")*/

    $.fn.piece32 = function (options) {
        var defaults = {
            facade: true,
            active: false
        };
        var options = $.extend(defaults, options);
        return this.each(function () {
            var $piece = $(this);

            if (options.facade == true) {
                $piece.find(".head").followParent32({ cutWidth: 6, top: 2, left: 2 });
                $piece.find(".title").followParent32({ left: 8, top: 3 });
                $piece.find(".del").followParent32({ right: 4, top: 5 });
                //                $piece.find(".pBody").followParent32({ left: 2, bottom: 2, cutWidth: 4,
                //                    cutHeight: $piece.find(".head").height() + $piece.find(".tool").height() + 7
                //                });
                $piece.find(".body").followParent32({
                    left: 2,
                    bottom: 2,
                    cutHeight: 30,
                    cutWidth: 4
                });
            }

            if (options.active == true) {

                //-----------------------允许拖动
                $piece.draggable({
                    containment: "parent",
                    cursor: "crosshair",
                    handle: ".head",
                    opacity: 0.60
                });

                //-----------------------允许拉伸
                $piece.resizable({
                    minWidth: 150,
                    minHeight: 150,
                    maxWidth: 1000,
                    maxHeight: 1000,
                    animate: false,
                    resize: function (e, ui) {
                        $piece.piece32();
                    }
                });

                //-----------------------删除按钮的鼠标活动
                $piece.find(".del").hover(function () {
                    $(this).addClass("del_over");
                }, function () {
                    $(this).removeClass("del_over");
                }).click(function () {
                    if ($piece.find(".body").html() == "" || confirm("是否永久删除该笔记块？")) {
                        $piece.remove();
                    }
                });

                //-----------------笔记块右键
                $piece.find(".head").contextMenu("menu_piece", {
                    bindings: {
                        "menu_piece_ReName": function (item) {
                            $piece.find(".title").reName32({
                                className: "titleEditTextBox",
                                focusout: function (ui32) {
                                    ui32.rename();
                                }
                            });
                        }
                    }
                });

                //-----------------笔记块头双击改名
                $piece.find(".head").dblclick(function () {
                    $piece.find(".title").reName32({
                        className: "titleEditTextBox",
                        focusout: function (ui32) {
                            ui32.rename();
                        }
                    });
                });

                //-----------------点下后显示在最前
                $piece.mousedown(function () {
                    var newIndex = 2;
                    $("#windowInner").find(".piece").each(function () {
                        var Index = parseInt($(this).css("z-index"));
                        if (newIndex <= Index) {
                            newIndex = Index + 10;
                        }
                    });
                    $piece.css({ "z-index": newIndex });

                    $piece.pieceFocus32();

                    //alert($piece.find(".body").parent().html());
                });
            }
        });
    },

    $.fn.pieceFocus32 = function (options) {
        var defaults = {}
        var JQself = $.extend({}, defaults, options);
        return this.each(function () {
            var JQself = $(this);
            var JQpieces = $("#windowInner .piece");
            //--------其它的块失去焦点
            JQpieces.find(".head").hide();
            JQpieces.find(".del").hide();
            JQpieces.css("border", 0);
            JQpieces.find(".body").css("border", "1px solid #ddd");
            JQpieces.css("background-color", "transparent");
            //--------本块提到焦点
            JQself.find(".head").show();
            JQself.find(".del").show();
            JQself.css("border", "1px solid #aaa");
            JQself.find(".body").css("border", "0px solid #ddd");
            JQself.css("background-color", "#fff");
        });
    }









    //    ,/*
    //    *pieceActive_ 0.1
    //    *Copyright (c) 2012 AtHome
    //    *Date:2012-10-22
    //    *可以轻易设置笔记块的外观与功能
    //    */
    //    /*------------------------------------------------------使用者是piece*/
    //    $.fn.pieceFacade_ = function (options) {
    //        var defaults = {}
    //        var options = $.extend({}, defaults, options);
    //        return this.each(function () {
    //            var JQself = $(this);
    //        });
    //    },

    //    /*------------------------------------------------------使用者是piece*/
    //    $.fn.pieceActive_ = function (options) {
    //        var defaults = {}
    //        var options = $.extend(defaults, options);
    //        return this.each(function () {
    //            var JQself = $(this);
    //        });
    //    }



})(jQuery);